Before the advent of mechanical lawnmowers a commonly-used way to keep grass trimmed was to allow livestock, usually sheep, to graze; a ha-ha allowed them to trim the grass of large estates while keeping them out of view from the house.
The ha-ha is a feature in the landscape gardens laid out by Charles Bridgeman, the originator of the ha-ha, according to Horace Walpole (Walpole 1780), and by William Kent and was an essential component of the “swept” views of Capability Brown.
The contiguous ground of the park without the sunk fence was to be harmonized with the lawn within; and the garden in its turn was to be set free from its prim regularity, that it might assort with the wilder country without.
Walpole surmised that the name is derived from the response of ordinary folk on encountering them and that they were, “…then deemed so astonishing, that the common people called them Ha! Has! to express their surprise at finding a sudden and unperceived check to their walk.”
An unusually long example is the ha-ha separating the Royal Artillery Barrack Field from Woolwich Common in southeast London. This deep ha-ha was installed around 1774 to prevent sheep and cattle, grazing on Woolwich Common as a stopover on their journey to the London meat markets, from wandering onto the Royal Artillery gunnery range.
Ha-has were also used at Victorian-Era lunatic asylums such as Yarra Bend Asylum and Kew Lunatic Asylum in Australia. From the inside, the walls presented a tall face to patients, preventing them from escaping, while from outside the walls looked low so as not to suggest imprisonment.
* This is just an abbreviated version of the current Wikipedia entry for Ha-ha, I didn’t write any of it.