In baseball, a player can replaced by any other player who hasn't yet appeared in the game. Can this replacement be made after a player has started batting and is still at the plate?
Absolutely. From the 2018 MLB 0fficial Rules:
A player, or players, may be substituted during a game at any time the ball is dead.
When a batter leaves the game with two strikes against him, and the substitute batter completes a strikeout, the official scorer shall charge the strikeout and the time at bat to the first batter. If the substitute batter completes the turn at bat in any other manner, including a base on balls, the official scorer shall score the action as having been that of the substitute batter.
Basically, any player, offense or defense, can be substituted for another, when the ball is not "in play."
The basic reason is injury. Suppose (and this would be a freak accident), a batter was hurt while hitting a foul ball. The manager might want to use a pinch hitter, rather than having the hurt batter continue to bat and run.
The new batter would inherit the ball-strike count of the old batter. As mentioned in another post, the new batter would be credited with the ultimate result EXCEPT when the original batter had accrued two of the three strikes in what ultimately becomes a strikeout.