Can I attach a 40 spoke hub to a 36 spoke rim?
"Perverse" is what Sheldon Brown calls this sort of wheelbuilding, but it apparently can be done. There are even video tutorials for a 32-36 mismatch, which might help you. Unless you really need a sturdy rim, you could look into using a 32-hole rim as a 4:5 pattern might be simpler than 9:10. You need several sizes of spoke for such a build. 

Another option worth a look is trying to get undrilled rims, though they're probably expensive.

I have heard of people building wheels ignoring the spoke hole offset from the centreline, or filing out the holes if the offset is slight. Doing that on a 20-hole rim would mean you'd have to drill every other hole - but be careful as the valve hole could easily be a problem (most but not all of my wheels have it halfway between spokes). I'd be wary of such an approach personally.

I don't suppose the front is 40h is it? Building a new front wheel and putting the old front rim on the back would be a neat solution if it is, but 40h front wheels aren't common.