I do this, but the requirements are a little more than just setting up a domain name.
Firstly, you need to be running your own DNS server, or your router/modem that is doing DNS needs to be capable of routing internal traffic back into the LAN when it detects local traffic trying to connect to the external IP address of the local network.
The router I have isn’t quite smart enough for that, so I do things slightly differently.
I have my network update a free no-ip address. This network name never changes, but the IP is dynamic, so this is the first requirement.
On my personal domain I create a subdomain that I call my “home domain.” Then in this subdomain I have a CNAME record that points to my no-ip address. This means that I can use my personal domain hostname to connect to my home network from anywhere on the internet. It allows for a few other benefits but that’s outside the scope.
So cname.sub.domain,com points to hostname.no-ip,com
(A little secret, you can make the CNAME be “cname.sub” to save you setting up a new subdomain on your external domain. Also, yes, I am aware of the commas. New user. Apparently I can’t post links to domains yet)
That’s great for outside your network, but unless your router is as described above, your local devices on the inside of your network won’t work.
To resolve this, you need to run a local DNS server that becomes the primary DNS for your local network. I haven’t done this with a Pi-Hole.
Essentially, you need to create a local DNS server that is going to be authorative to your local network for your subdomain. I set it up to be authorative for my sub.domain,com and forward for everything else.
You then need to adjust your DHCP settings in your router (or whatever device is doing DHCP) to make this new DNS server the primary DNS server for devices on your network. Make the previously configured DNS server the secondary in your DHCP settings. This is often the router itself.
Then, once I have that working, I create A (ipv4) and AAAA (ipv6) records for that CNAME hostname that points to the internal IP address of my Home Assistant setup.
So from inside you’d see cname.sub.domain,com point to your local network IP address of your HA.
From the outside, you’d see cname.sub.domain,com point to the external IP address determined by (in my case) no-ip,com.
Insert the usual port forwarding rules as you are already aware of them.
If you’re running on something a bit stronger than a Raspberry Pi, I recommend setting your DNS up as a Docker container. That makes security upgrades and the rest a lot easier. I am old fashioned and still use bind9 myself, but there are a lot of choices when it comes to DNS servers. I’m sure others can recommend something they might prefer.
I know this is a massive wall of text, so if you’d like me to elaborate on or clarify anything, just let me know.