You can still often see ha-has in the gardens of British stately homes. They usually consist of a sunken wall with its top at garden ground level, bounded with a ditch on the outer side.
The ha-ha at Montacute House, Somerset
This stops cattle or sheep getting into the gardens without interrupting the view from the house. An older — more usefully descriptive — name was sunken fence. The class-related implication of this boundary is obvious from this quotation:
Two marquees had been erected for these two banquets: that for the quality on the esoteric or garden side of a certain deep ha-ha; and that for the non-quality on the exoteric or paddock side of the same.
Barchester Towers, by Anthony Trollope, 1857.
So far as we can tell, the word was originally French. When it crossed the Channel into Britain, it was variously spelled at first, often as ah ah, and it seems certain that the idea of a surprise was intimately associated with it. If you stumbled across it in the dark you certainly wouldn’t think it at all funny.