Why does SpaceX attempt sea recoveries of the Falcon Heavy center core instead of staying at altitude and performing a land based recovery at a different site?
Because of accidents of geography and history.
Both of the main US launch sites are where they are because rockets occasionally crash, and, for fully fueled heavy lift vehicles, worst-case impacts have yields approaching that of nuclear detonations.
So they were sited to fire out over the ocean for the longest possible distances to minimize potential casualties.
SpaceX continues to launch from these sites, which means that, to reach land, they would be traversing the entire Atlantic for landings in Africa or Europe, or across the Pacific into Easter Island or Antarctica.
It would certainly be possible to design a rocket that flew this sort of distances, but the energy needed is more than normally provided by a first stage and less than that from the second, and it would be coming down at speeds where atmospheric heating starts to require additional weight in protective systems.
So you end up with a first stage that needs to be nearly a SSTO AND capable of re-entry. So far it seems the math works out better with splitting first and second stage at the point were re-entry heating is more manageable and accept the mid-ocean landing.
It would also be possible to change the launch site, for example launch out of the US Midwest and recover in Florida. This would require someone to accept the risks of a crashed stage. China does just this, but does so as a government. SpaceX is a private company in the US so would need a complex mix of insurance and certification and probably still face lawsuits delaying individual launches.
A minor advantage of barge landing is that it makes getting the stage back to launch site easier. For a cross-country launch, you need to either design for road/rail transport or work out how to fly them. Some hypothetical discussion around SpaceX has included refueling and servicing landed stages and just hopping them back to the launch site under their own power. This requires great confidence in the rocket reliability and halves the number of paying launches per engine.
So it would make a lot of sense to recover stages on land, but would require the right mix of launch site with good industrial support, clear space to launch into, a correctly-located landing site at the end of that clear space, and a method to get the stages back. And for SpaceX (but not necessarily competitors) also a place a US rocket company can legally operate.
It is purely down to trajectory. The side cores detach relatively early after the launch. This means that they are not so high nor do are they travelling so fast Eastbound. There is therefore enough fuel left in the side boosters to allow the "boost back burn" that allows them to change direction and get back to the original landing site.
The centre core detaches from the second stage much later. By this point it's much further out to sea, higher and travelling faster Eastwards. On Falcon Heavy, it's launching a heavy payload so it's important that they maximise the use of the fuel in the centre core. This means that it doesn't have enough fuel in to change its direction and velocity so that it gets all the way back to the Cape. Instead, they leave it on a ballistic trajectory so that it will naturally end up very near the drone ship. They can therefore land it with very little additional fuel requirement.
It would be possible for the centre core to come back to the Cape... but only if they saved lots of fuel in it. That means a much smaller payload and a lower orbit.
The ballistic trajectory to the drone ship therefore gives them a larger payload, higher orbit and a good chance to reuse the centre core...
Falcon is launched over sea, partly for safety reasons (to avoid rocket stages falling on people's houses - which happens in China). The first stage is in a ballistic trajectory and simply does not have the range to reach solid land before falling back to Earth. Only the second stage reaches orbital velocity - which ironically means it can't be recovered at all as it is travelling so fast (orbital speed) it is destroyed on re-entry.