What's perhaps more reasonable is to replace it with maybe 20 trains. You could maybe run such trains on a 10-minute headway, so 6 trains an hour, so 3 1/3 hours.
That's to replace one ship.
But it's also if you only have a single track rail line. You could build N parallel tracks along the same route. (This is far more reasonable than trying to build something where you can stack the containers 10 high or something, and then wide enough that it would be stable, and then you have to worry about the load-bearing ability of the track.)
But even if you build all those tracks, then you have the problem of unloading the ship at one end, and reloading a different ship at the other end. You can build enough tracks to get any rate you need, but can you get the load/unload speed?
We're talking about Mexico right?
But running the numbers on it, you really can't compete on throughput, what with the truly massive capacity of cargo ships. Like, even if it had only 3000 conatiners, it only has to go at 1/1500th of the rail speed (assuming you can stack them two-high) to beat it. This page[1] says panamaxes have traditionally held 5000, with "New Panamax"es carrying 13,000.
(Edit: ChatGPT tells me that transit of the canal takes 10 hours on the high side, and it's 82 km long, so that's an effective ship speed of 8.2 km per hour, meaning the rail would have to go 20,000 km/h to compete on throughput.)
But yeah, the other advantages you mention would apply.
[1] https://en.wikipedia.org/wiki/Panamax?useskin=vector#Cargo_c...