There are several different ways to approach this sort of situation. This approach of using scales is more generally referred to as Chord Scales. If you were to play these scales to accompany these chords by starting with the root in any given place, they are more likely to sound less smooth within your solo, though not necessarily. If you are jumping from one to the next, as opposed to making your line on one scale move toward the start of the next scale, then the lines will not sound very connected; it will sound more like you have one idea, then move to the other scale for the other idea. You would be playing all of the "correct" notes for those chord scales but it would sound chunky. The approach you suggest of using D Locrian could sound interesting but you are mistaken that this scale contains the same set of notes as B Dorian. D Lydian contains the same notes as B Dorian. If you were to use D Lydian, you're more likely going to have a line that is smooth going from one to the other, as you're relating them both to a single starting point.
The thing to avoid is thinking of starting each of these ideas at the root of the scale every time the chord changes. This can make things feel disconnected or very dry/bland. Generally speaking, the solos that speak to us the most as listeners are the ones that don't feel like they are relying on scales for their content and instead are just melodic lines or riffs that exist on their own and just happen to contain the notes in the chord scales. This is difficult to do early on, as you are less familiar with such an approach, but does become easier with practice. You want to become familiar with the notes that make up each scale and how they fit together with each other. Which notes do the two different scales have in common and which are different? How would you move each note that is different to the other note?
It's important to note that this is a very sterile way to approach everything, as it's less about what sounds good and more about what is "acceptable" or fits a given "rule". The idea is to learn these things to become familiar with the approach, then, hopefully, forget about it all and just play what sounds/feels good. For most people, you don't want to be thinking about scales and rules when you're improvising; you want to just know everything well enough that it is a part of your vocabulary. It's like learning a foreign language. At first you have to try to remember specific words and how to conjugate verbs and which order your nouns and adjectives are supposed to appear, but once you're familiar enough with the language, those things happen automatically.
If I were to break down these two scales, I would make a chart to see these differences, somewhat like the below. I will be using the same starting point for each scale so that we can do a side by side comparison.
D Dorian: D-E-F-G-A-B-C
B Dorian: D-E-F#-G#-A-B-C# (Same notes as D Lydian)
We can see that there are 4 notes in common and the other three are all raised a half step. This allows you to see that as you are going through your lines, if you are trying to stick to chord scales, you will want to be aware of the notes that remain the same. For example, if I'm on F on the Dm, I can move to F# on the Bm. Emphasizing the notes that change between the two scales can add a lot of color but can also sound less cohesive. Emphasizing the notes that the two scales have in common will make the linear transitions sound a bit smoother. You want to try to get in touch with how using the notes that are different from scale to scale impact the overall feel of what you are playing and actively choose whether it is best to emphasize the similarities or differences. It can also add a lot of variety to your solo/melody if you utilize both approaches, sometimes emphasizing the common tones, other times emphasizing the differences.
Again, these are exercises that have the goal of ingraining these concepts into your vocabulary. A lot of people will criticize this approach, suggesting that it's too academic and takes away from the art, which it could, but it is a perfectly acceptable approach to learning these concepts.