When It Comes to Speed
When it comes to runtime in particular, what generally everyone else is saying is right; there usually is a hidden constant whenever you refer to a function in Big O. If the function with the O(n^2) had a relatively small constant and didn't run particularly long, it could be faster than a function which ran O(n) with a large constant and ran longer.
Don't Forget About Memory
Runtime isn't the only thing to consider when writing or using an algorithm; you also need to worry about the space complexity. If you happened to need to conserve memory in your application and you had to choose between a function which ran in O(n) but uses a ton of memory and a function which ran in O(n^2) but uses much less memory, you might want to consider that slower algorithm.
A good example for this is quicksort vs. mergesort - in general, mergesort is consistently faster than quicksort, however quicksort is done in place and doesn't require allocating memory, unlike mergesort.
In Conclusion
Is it sometimes better to write solution in O(n^2) than in O(n)?
Yes, considering the specific circumstances of your application, the slower option may indeed be the better one. You should never rule out an algorithm simply because its slower!