Yes, that has actually happened on a United flight from Des Moines, Iowa, to Denver, Colorado; the captain was incapacitated and had a suspected heart attack.
The first officer actually spoke over the PA and asked for a doctor and somebody with [sic] flight experience.
Luckily Air Force pilot Capt. Mark Gongol was on that flight so he rushed to the cockpit. He took over some of the checklists and Radio/ATC Comms so that the less-experienced first officer could make the landing with less stress.
A safe landing was made by the first officer, the captain was rushed to the hospital by ambulance for treatment and later survived.
Interestingly Capt. Mark Gongol, pilot,bflew a B1-B in the Air Force, which was much heavier, faster and more sophisticated than a Boeing 737, so technically with minimal training, he should be able to land the plane safely on his own.
Anyway, it was a perfect day for the fellow passengers to say, “Thank you for your service!” and pay some respect, which all servicemen and women deserve.