If using the correct wieght of the yarn, is it necessary to use the fabric recommended in the pattern?
If you mean the brand suggested on the pattern, no it isn’t necessary but keep in mind that the pattern has given all sizing and yarn amounts used based on what was achieved using the recommended yarn.
I usually substitute the called for yarn for what I have in stash, without any problems
No, I mean if the pattern calls for linen or cotton, can substitute silk or polyester as long as the weight is the same?
It should be fine, if in doubt do a swatch first to see if it matches and if you like the fabric. As long as you’re using the same weight yarn there shouldn’t be a vast difference.