Can I spray foam up to roof sheathing when air sealing skylight tunnel?
You can use spray foam as long it is a minimal expansion type of foam.  Some brands call it door and window foam. This is usually only a 3 to 1 or 4 to one expansion factor. If you use the more aggressive expansion foam, it can cause deflection of the joists and actually cause leaks or cracked windows. Don't overfill the gaps, spray an even bead, let it cure, if you still have a lot of air space, you can put in a second bead of foam.