Some options:
1. Just let him win D&D. He now gets to spend the rest of his life as a Hydra, until someone casts dispel magic with a 9th level slot or makes a DC 19 ability check with it. Depending on the group this might be a trivial issue, or a huge obstacle.
2. Limit the number of heads a hydra can have. Only so many can fit in a limited space anyway. Also I don't really understand how they did this beheading. A hydra has a net loss of 5 HP each round it regrows heads, and that is a minimum if you manage to do exactly 25 damage (not an easy feat in D&D). By my math you could only get up to 33 heads without magical healing if you were delivering near perfect doses of 25 damage per 6 seconds. That's still more than I would allow, but it makes this more in the realm of standard necromancer player cheese rather than instant win-button and we-can't-actually-roll-all-these gamebreaking. And honestly between bringing a 33 headed hydra to a fight that wasn't against a bunch of mooks and bringing a 17+ level Wizard, I'd choose the Wizard.
3. Have the BBEG be one of the numerous creatures immune to non-magical piercing damage. You can still let the player steamroll a bunch of minions and feel awesome without letting them necessarily lay a point of damage on the main villain. Remember that any humanoid can contract lycanthropy, and gain this protection.
4. Have the BBEG be capable of flying out of the 10 foot reach of this land-bound creature. Once again, let the player steamroll other enemies and feel awesome, maybe even let them finish off the villain once the group knocks them out of the sky.
5. Have enemies target the hydra. It only has 172 hit points if you remember to use fire, is a big target, and probably has the lowest AC in the group. It can kill anything not immune to its attacks that it gets in range with, but it can only get in range with so many enemies in the course of a combat. Focused fire could end it real fast.
6. Target the hydra with practically any spells. It has crap saves on anything it doesn't get advantage against. Slow would be the funniest to hit it with successfully (it can now make 1 attack), but targeting Charisma or Intelligence saves is more likely to land. And, while it's not a save, it is incapable of making the DC 20 Int ability check to escape Maze. The fight can become about breaking the BBEG's or a spellcaster minion's concentration so that the hydra can get back in the action and go to town.
7. Let it do some damage, then target the hydra with a level 9 Dispel Magic.
I wouldn't ban True Polymorph, as, aside from requiring retconning, that is a rather extreme solution to a powergaming tactic that really isn't as ironclad as it at first seems, and you have reached the levels where 5e D&D isn't really balanced and where the campaign is presumably reaching its end anyway. Players have slogged through a lot of levels to throw 9th level magic around, and they should get to reap rewards, the rewards just can't be allowed to trivialize the climactic battles of the campaign. I would instead reward creative play and extensive levelling by making the thousand-headed hydra useful, but maybe not as useful as it at first seems, and avoid letting it hog all the glory in the final encounter.