Why is the Panama Canal located in Panama?

Well the original plan was to build a sea level canal in Nicaragua, but the route was decided to be too long and expensive. Panama offered a shorter and cheaper route, although it would involve crossing mountains that were not a problem on the route in Nicaragua.
The idea of a canal in Nicaragua quietly died until in the late 1950s when the US proposed blasting the canal channel using roughly 100 "clean" hydrogen bombs all detonated simultaneously along the route of the canal as part of Project Plowshare which was then seeking peaceful uses of nuclear explosives. This would have drastically reduced the construction costs (by perhaps a factor of more than 100). But the idea was effectively killed by the 1963 Atmospheric Test Ban Treaty.
There are still plans to someday replace the Panama Canal with a new much wider "Nicaragua Canal", but the costs (and political instabilities in the area) keep getting in the way. Also there are several ecological concerns with this route that must now be addressed that would not have been considered issues in the 1960s or before.