If a player serving as the DH is later used on defense, he continues to bat in his same lineup spot. But for the rest of the game, his team cannot use a DH to bat in place of the pitcher. A team is also barred from using a DH for the rest of the game if the pitcher moves from the mound to another defensive position, a player pinch-hits for any other player and then becomes the pitcher, or the current pitcher pinch-hits or pinch-runs for the DH.
https://www.mlb.com/glossary/rules/designated-hitter-rule
MLB Rule 5.11 found on page 59 of the 2021 MLB rule book. https://img.mlbstatic.com/mlb-images/image/upload/mlb/atcjzj9j7wrgvsm8wnjq.pdf