I've a direct mount rear derailleur and when I tighten the thru-axle the rear derailleur moves inwards towards the spokes, around 5mm. I'm getting recurring problems with gearing (I frequently take the rear wheel off to put the bike on a trainer) and have to adjust the high limit often. The derailleur hanger seems flush with the frame when the wheel is off, and the bolt above and to the right of the thru-axle is tight. The bike is a couple of months old. I wanted to ask if the movement when tightening the thru axle is expected?
Yes, it's normal or at least common for the derailleur to move in this way as the thru axle is tightened. QR rear ends often do the same thing as any gap between the hanger and frame is drawn up. In a perfect world the parts would all interface tightly even without the tension on them, and some frames/hangers are in fact better at that than others. Basically the ones that have everything machined and unpainted, but that's not the norm.
The hanger must be aligned so that it holds the derailleur in correct alignment when the axle is tight. The system won't work right otherwise. For all the contemporary systems, assume there is no acceptable tolerance; it will always work better the closer to perfect you can get it. So in other words you probably just need to get the hanger aligned. Sometimes on bikes that have heavy squirm of the hanger as the axle is tightened, even a new hanger won't be in correct alignment until it's manually aligned, which is to say that just ordering a hanger up isn't necessarily a solution. DM hangers align the same as any other.
If you're getting the impression that the alignment is changing by itself as time goes on, that could indicate it's settling in as a painted surface in the interface of the parts gets worn in. Some designs also have o-rings inside for retention that could be out of place and squirming around. You could take it all apart, gently sand smooth any paint inside that you can see is getting worn away, and lubricate the axle threads to see if that gives any improvement. Nothing should be changing such that you must repeatedly adjust the limit screws.
The direct mount hanger on your bike is heavily skeletonized. It will bend much easier with impact than a non-skeletonized version of the same material. It's probably too trick for its own good. If you have repeat problems with the hanger getting tweaked, you could consider getting the non-direct-mount (aka normal) hanger version for whatever frame this is plus the b-knuckle parts for your derailleur. (I believe that by nature the b-knuckles for all the applicable Shimano derailleurs interchange since they also all work on the same DM hangers, but I haven't messed with this in practice). That might improve things.