Hot take: Usually makes it worse.
Oh man, that's the old school way to do it and it's pretty much a disaster. It seals all the moisture in like a plastic bag, so the wood just rots out behind the foam. These days, the best practice is often to just leave the cavity open, maybe put in a drain at the bottom, and let the tree compartmentalize the damage itself. Filling it mostly just helps the people who see it feel better, not the tree.
Honest question, does it really matter that much for a tree in someone's backyard? Like, sure, if it's a big old oak on a busy street, maybe. But most of these cavities are small and the tree just deals with it. I've seen trees with big gaping holes that have been fine for decades. Feels like arborists get a little overprotective sometimes, don't you think?