There's nothing that says you need to use the exact yarn the pattern calls for. (In fact, we rarely recommend it!) But there are a few things to keep in mind when you substitute yarn.
There are two main systems of categorization for yarn weight: the fingering/dk/worsted/etc system, or the numeric system.
Most yarn you find in a LYS will use the first system, while commercially produced yarn or yarn found ina. box store will use the numeric system.
This is usually the most important thing to get right when substituting. If you're using a pattern written for worsted weight but want to use a DK weight yarn, that's okay, but you're going to need to do a little math first (and possibly adjust your needle size) to make sure the gauge works out.
Of course there are exceptions to every rule. Scratch-favorite Woolstok, for example, is worsted weight... but it's so light that we use it interchangeably with DK.
2. Fiber Content
How important is fiber content? It depends on what sort of thing you're making!
Some substitutions can be made depending on the project's intended use.
Fingering weight yarn and sock yarn are the same, weight-wise, but they have one important difference: sock yarn contains nylon (or or an equivalent fiber) for durability. You can always swap sock yarn for fingering weight yarn, but if you want to make socks specifically, you might want to be sure your yarn has nylon blended in for durability.
Or maybe you're looking at a design in which the designer chose to knit a baby blanket out of gorgeous alpaca yarn. It's probably beautiful! But before you grab your needles, take a moment to consider that project's future: if the baby blanket you're making is likely to end up in the hands of an actual baby, you might want something a little more laundry-friendly than alpaca.
Some types of fiber--eg, silk--lend a particular type of drape or sheen to a garment. If a designer has chosen a silk blend for their design and that's part of what you love about it, then you might want to find a similar blend in your yarn selection.
Generally speaking, the more unusual the animal, the more delicate the fiber. The hair of the arctic musk ox is spun into quiviut. Quiviut is gorgeous: it's incredibly soft, warmer than anything else you can think of... and needs to be babied. Acrylic yarn, on the other hand, is as indestructible as plastic (because it IS plastic!).
What about superwash/non-superwash?
What IS superwash?
Superwash is a treatment that descales fiber. It's a little like conditioning your hair, but permanent. The superwash process means that you can throw your finished project in the washing machine (and maybe even the dryer - we're brave knitters around here, but we're not fearless!) and it won't felt or shrink. This is great for things like socks, kid knits, tshirts... anything you think you might want to launder more than once or twice a season.
Non-superwash yarn tends to hold up a little better over time, and shows texture better than superwash yarn.
A designer might choose a superwash or non-superwash yarn for their design, and you might or might not decide to do as the designer suggests in your creation.
3. Dyeing style
Indie dyed, speckled, dyed in the wool, marled... there are so many styles of yarn out there! Which one you choose depends on the project you're working on and how you want your final result to look.
Speckled/indie dyed yarn can be a great way to "let the yarn do the work for you." If you're knitting a straightforward sweater, such as Tin Can Knits' Flax, you might want to choose something speckled for a a little visual interest. (If, of course, it fits your personal aesthetic - don't forget that you're going to want to wear this sweater after you do all the work to create it!)
Some patterns, such as Andrea Mowry's Find Your Fade, are written with speckles in mind. The speckled dyeing technique is what lets the skeins "fade" from one to the next. If you were to knit this project in tonal or solid yarn, you'd end up with stripes at the transition points. (Stripes are great! So it's just a matter of deciding what you want your result to be.)
If you're doing something textured or cabled, you might want to stick with solid or tonal yarn. This will let all the hard work you're doing in creating the texture stand out in the end result.
And then there's marled yarn. Marling is a handspinning technique (which our friends over at Spincycle have figured out how to produce on a commercial scale) that allows for transitions from one color to the next within a single skein. This technique can be replicated at home by holding multiple skeins together to create the weight that the pattern calls for. Marled yarn can be an amazing choice for the contrast color in a colorwork yoke.
There is always a way to create the project you have your eye on at a price point that works for you. That's where your Local Yarn Shop comes in!
We're always happy to spend time talking through pattern priorities and budget with you to figure out where you can cut costs without losing the aspects of the project that drew you to it.