Using WITH and IF in the same query
It may help you more if you think about it, not as a WITH statement but as a Common Table Expression (CTE).

That way you can go back to the Microsoft Documents on CTEs and read the following line;


  A CTE must be followed by a single SELECT, INSERT, UPDATE, or DELETE
  statement that references some or all the CTE columns. A CTE can also
  be specified in a CREATE VIEW statement as part of the defining SELECT
  statement of the view.


Although technically you can define a CTE and then not reference it at all but then what would be the point in defining it? e.g.

WITH Missing AS (SELECT 'The Point' AS What)
SELECT * FROM sys.messages;




Going back to your case, you could rewrite it using CASE statements instead of IF statements but again what's the point? You don't need them.

You're query can be re-written to:

DECLARE @device char(3) = 'en3',
        @other_parameter varchar(3) = 'yes';
WITH measurements AS (
                         (SELECT    *
                          FROM  measurement_database1)
                         UNION ALL
                         (SELECT    *
                          FROM  measurement_database2)
                         UNION ALL
                         (SELECT    *
                          FROM  measurement_database3)
                     )
SELECT  *
FROM    measurements
WHERE   device LIKE @device
  AND   op LIKE @other_parameter;


So there's no need of IF statements at all!
HTH