I put in a new window and need to patch up the wall around the installation. I noticed that the cellulose insulation in the walls doesn’t completely fill the cavities. Can I just spray some cans of spray foam or do I need to do something else?
Do some flame-testing of anything you put in your house like that. DIY canned foam products tend to have much, much worse fire characteristics than the commercial mixed-on-site 2-part stuff used by professionals. Not to suggest that stuff is particularly good.
It isn't just that it's a fire accelerant. When it burns, even if exposed to flame source from elsewhere, it emits toxic fumes that will incapacitate you and prevent your escape. This usually involves cyanates, that stuff that caused all the trouble in Bhopal, India.
Test the stuff yourself, outside, with the wind blowing away from you. Or just watch this video