When does middlegame switch to endgame?


There's no quick and easy definition that works for all cases. In general it's when there's not enough pieces to threaten a direct checkmate, so the game takes on a strategic character where players maneuver to advance their pawn majority and/or target weak enemy pawns. Often the kings are safe to come to the middle and participate in the fight.
As a few counter examples, you'll hear people say "the Berlin endgame" as early as move 9 when it's definitely not safe to bring the kings out yet, and also a Q+R vs Q+R endgame you'll still want your king tucked away behind pawns.
So some positions linger between a late middlegame and early endgame... but yeah, I'd say it's the endgame when both players are more concerned with eventually queening a pawn than with any ideas of checkmate.