I googled "why we always want what we can't have" and most of what came up was related to romance, which wasn't the only thing I was thinking about when writing this article. I think this behavior can apply to many different aspects of our life, and so it is all the more important to be aware of this tendency because although I, unfortunately, couldn't find a "cure" for this behavior, I believe that being aware of a problem is the first step towards solving it.
I think the answer to this question comes down to daydreaming. The wonderings and making up of scenarios of an idle brain. That cheeseburger that you have been craving for weeks, imagining what it's going to taste like when your diet finishes, is never going to be as perfectly tasty in real life as it was in your head. Don't get me wrong, its probably going to be amazing, but no cook can make something exactly as it is in your imagination, simply because your imagination is something so uniquely yours.
This cheeseburger can be used as a metaphor for other things in life as well. A vacation, a test you studied hard for, or even a relationship. Anything, really. And again this doesn't mean that life is depressing and you are never going to get things your way. It all comes down to expectations usually differing from reality, which can happen for the best as well.
There is a psychological phenomenon called "impact bias" which is the tendency we have of overestimating emotional states. Basically, when we are sad we think we are going to be sad for longer than we actually are, the same goes for happiness and other emotions. So possibly the impact we think obtaining something would have on our happiness is bigger in our heads compared to reality, and so when we actually get the thing we are disappointed by how fast the rush of happiness of having it passes. This is probably why many psychologists advise their patients to focus on the things they are grateful for in order to live a happier life.
So by wanting what you can have you get to live only with the picture of what having that thing would be like, and this way your high expectations are never crushed. Whether this is a subconscious defense mechanism or just a brain malfunction, I find that this happens to many people, and affects various different aspects of life.